using System;
using System.Text;
using SUnit.Core;

namespace TestDinamico
{
	[SUnitTest]
	public class TestArreglos
	{
		[SUnitTestCase]
		public static void TestSumarArregloaExitoso()
		{
            CValue[] parameters ={DynamicTest.GetArray(new int[]{4,5,2,3,1}),new CInt(5)};
            CValue res = DynamicTest.InvokeFunction("sumarArreglo", parameters, CTypes.Int);
            DynamicTest.AssertEquals(new CInt(15), res);
		}

        [SUnitTestCase]
        public static void TestArregloDevuelto()
        {
            CValue[] parameters ={DynamicTest.GetArray(new int[]{4,5,2,3,1}),new CInt(5)};
            CValue expected = DynamicTest.GetArray(new int[] { 5, 6, 3, 3});
            DynamicTest.AssertEquals(expected, DynamicTest.InvokeArrayReturningFunction("sumarUno", parameters,expected));
        } 
	}

    [SUnitTest]
    public class TestStrings
    {
        [SUnitTestCase]
        public static void TestStringsConDiferenciasPersonalizadas()
        {
            CValue expected = new CString("Este is a string ment for testing+");
            CValue res = DynamicTest.InvokeFunction("reemplazar", new CString("Este es un string de prueba"), CTypes.String);
            //DynamicTest.AssertEquals(expected, res);
            AssertEqualsString(((CString)expected).Characters, ((CString)res).Characters);
        }

        [SUnitTestCase]
        public static void TestStringsExitoso()
        {
            CValue expected = new CString("Weberia estar todo bien");
            CValue res = DynamicTest.InvokeFunction("reemplazar", DynamicTest.GetCValue("Deberia estar todo bien"), CTypes.String);
            AssertEqualsString(((CString)expected).Characters, ((CString)res).Characters);
        }

        public static void AssertEqualsString(string a, string b)
        {
			if(!a.Equals(b))
			{
	            string diff = "";
	            for (int i = 0; i < Math.Min(a.Length, b.Length); i++)
	            {
	                if (a[i] != b[i])
	                    diff += "" + a[i] + ",";
	            }
	            string res = String.Format("\n   Esperaba:   {0}\n   Llego:    {1}\n   Caracteres diferentes:   '{2}'", a, b, diff);

	            throw new Exception(res);
			}
        }
    }
}
